package org.devocean.groupbuying.web.backend;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.devocean.groupbuying.domain.Box;
import org.devocean.groupbuying.service.BoxService;
import org.devocean.groupbuying.utils.RenderMapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = { "/gb-cpanel/boxes" })
public class BoxesController {

	@Autowired
	private BoxService boxService;

	@RequestMapping(value = "/")
	public Map<String, ? extends Object> list(
			@RequestParam(value = "node", required = false) String node) {
		try {			
			if (node.equals("root")) {
				List<Box> boxes = new ArrayList<Box>();
				boxes = boxService.findAllBoxes();
				List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
				int i = 0;
				for (Box box : boxes) {
					if (box.getBox() == null) {
						Map<String, Object> data = new ConcurrentHashMap<String, Object>();
						data.put("boxID", box.getBoxID());
						data.put("text", box.getTitle());
						data.put("title", box.getTitle());
						data.put("level", i);
						data.put("leaf",box.getBoxes().size() > 0? false : true);
						result.add(data);
					}
					i++;
				}
				return RenderMapUtils.getMapChildren(result);
			} else {
				Box root = boxService.findBoxById(Integer.parseInt(node));
				
				List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
				int i = 0;
				for (Box box : root.getBoxes()) {
					Map<String, Object> data = new ConcurrentHashMap<String, Object>();
					data.put("boxID", box.getBoxID());
					data.put("text", box.getTitle());
					data.put("title", box.getTitle());
					data.put("level", i);
					data.put("leaf",box.getBoxes().size() > 0? false : true);					
					result.add(data);
					i++;
				}
				return RenderMapUtils.getMapChildren(result);
			}

		} catch (Exception e) {
			e.printStackTrace();
			return RenderMapUtils
					.getModelMapError("Error retrieving Boxes from database.");
		}
	}

	@RequestMapping(value = "/all/")
	public Map<String, ? extends Object> listCombo() {
		try {

			List<Box> boxes = boxService.findAllBoxes();
			List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
			for (Box box : boxes) {
				Map<String, Object> data = new ConcurrentHashMap<String, Object>();
				data.put("boxID", box.getBoxID());
				data.put("title", box.getTitle());
				data.put("boxe.boxID", box.getBox() == null ? 0 : box.getBox().getBoxID());
				result.add(data);
			}
			return RenderMapUtils.getMap(result);

		} catch (Exception e) {
			e.printStackTrace();
			return RenderMapUtils
					.getModelMapError("Error retrieving Boxes from database.");
		}
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public Map<String, ? extends Object> edit(@PathVariable("id") Integer id) {
		try {
			Box box = boxService.findBoxById(id);
			Map<String, Object> data = new ConcurrentHashMap<String, Object>();
			data.put("boxID", box.getBoxID());
			data.put("title", box.getTitle());
			data.put("fgKey", box.getFgKey() == null ? "" : box.getFgKey());

			data.put("clauseSelect", box.getClauseSelect());
			data.put("contain", box.getContain());
			data.put("clauseFrom", box.getClauseFrom());
			data.put("clauseJoins", box.getClauseJoins());
			data.put("clauseWhere", box.getClauseWhere());
			data.put("clauseOrderBy", box.getClauseOrderBy());
			data.put("clauseLimit",
					box.getClauseLimit() == null ? "" : box.getClauseLimit());
			data.put("label", box.getLabel());
			data.put("link", box.getLink());
			data.put("auto", box.getAuto() == null ? 0 : box.getAuto());
			data.put("isHtml", box.getIsHtml() == null ? 0 : box.getIsHtml());
			data.put("html", box.getHtml());
			data.put("partialName", box.getPartialName());
			data.put("pageType", box.getPageType());
			data.put("cacheTime",
					box.getCacheKey() == null ? "" : box.getCacheKey());

			data.put("disabled",
					box.getDisabled() == null ? 0 : box.getDisabled());
			data.put("boxe.boxID", box.getBox() == null ? 0 : box.getBox().getBoxID());
			return RenderMapUtils.getMap(data);

		} catch (Exception e) {
			e.printStackTrace();
			return RenderMapUtils
					.getModelMapError("Error retrieving Menus from database.");
		}
	}

	@RequestMapping(value = "/", method = RequestMethod.POST)
	public @ResponseBody Map<String, ? extends Object> addOrUpdate(@RequestBody Box box) {
		Box newBox = boxService.addOrUpdateBox(box);
		Map<String, Object> data = new ConcurrentHashMap<String, Object>();
		data.put("boxID", newBox.getBoxID());
		data.put("title", newBox.getTitle());

		return RenderMapUtils.getMap(data);

	}

	@RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
	public void delete(@PathVariable("id") Integer id) {
		boxService.deleteBox(id);
	}

}
